<template>
  <div class="app-container">
    <el-form :inline="true">
      <el-form-item label="实例名" prop="instanceName">
        <el-input
          v-model="queryParams.instanceName"
          placeholder="请输入实例名"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间">
        <el-date-picker
          v-model="queryParams.startTime"
          type="datetime"
          placeholder="开始时间"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          :picker-options="startDatePicker"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间">
        <el-date-picker
          v-model="queryParams.endTime"
          type="datetime"
          placeholder="结束时间"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          :picker-options="endDatePicker"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="list"
      row-key="id"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="60" align="center"/>
      <el-table-column prop="name" label="任务节点名称" :show-overflow-tooltip="true" align="center"></el-table-column>
      <el-table-column prop="instanceName" label="流程名称" :show-overflow-tooltip="true" align="center"></el-table-column>
      <el-table-column prop="status" label="任务状态" :show-overflow-tooltip="true" align="center"></el-table-column>
      <el-table-column prop="assignee" label="办理人" :show-overflow-tooltip="true" align="center"></el-table-column>
      <el-table-column prop="createdDate" label="创建时间" :show-overflow-tooltip="true" align="center"></el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="examineAndApprove(scope.row)"
            v-hasPermi="['backlog:list']">
          待审批
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 审批对话框 -->
    <el-dialog :title="title" :visible.sync="open" v-if="open" width="500px" append-to-body>
      <historyForm :businessKey="businessKey" v-if="'leave'==definitionKey"/>

      <el-form :model="form" ref="form" label-width="100px" class="demo-dynamic">
        <el-form-item
          v-for="(domain, index) in form.formData"
          :label="domain.controlLable"
          :key="index"
        >
          <el-radio-group v-model="domain.controlValue" v-if="'radio'==domain.controlType" @change="change1">
            <el-radio v-for="(item,index) in domain.controlDefault.split('--__--')"
                      :label="index"
                     :key="index"
                      >{{item}}

            </el-radio>

          </el-radio-group>
          <el-input type="textarea" v-model="domain.controlValue" v-if="'textarea'==domain.controlType"
          ></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {importProcess,queryList,suspendDefinition,deleteDefinition} from "@/api/activiti/groups";
  import {waitedTaskList,formDataShow,formDataSave} from "@/api/task/task";
  import historyForm from "@/views/task/history/historyForm";
  import {deepClone} from "@/utils/index";
  import { getToken } from '@/utils/auth'

  export default {
    name: "backlog",
    components: {historyForm},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        modelVisible: false,
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 表格树数据
        list: [],
        //文件列表
        fileList: [],
        modelerUrl: "",
        // 菜单树选项
        menuOptions: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 总条数
        total: 0,
        // 查询参数
        queryParams: {
          pageNo: 1,
          pageSize: 10,
          startTime: '',
          endTime: '',
          taskCategoryId: '',
          userName: '',
          id: ''
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          categoryKey: [
            {required: true, message: "产品类别不能为空", trigger: "blur"}
          ],
          // categoryCode: [
          //   {required: true, message: "产品编码不能为空", trigger: "blur"}
          // ],
          categoryName: [
            {required: true, message: "产品类别名称不能为空", trigger: "blur"}
          ]
        },
        startDatePicker: this.beginDate(),
        endDatePicker: this.processDate()
      };
    },
    created() {
      this.getList();
    },
    watch:{
      total(val,oldVal){
        /*  第N页最后一条数据删除后 重新加载上一页 */
        if(this.total>0 && this.total%this.queryParams.pageSize === 0){
          this.queryParams.pageNo = this.queryParams.pageNo - 1
          this.getList(this.queryParams)
        }
      }
    },
    methods: {
      beginDate(){
            return {
                disabledDate: time =>{
                if (this.endTime) {
                   return new Date(this.endTime).getTime()> time.getTime()
                    
                } else {
                    return time.getTime() > Date.now()
                }
                }
            }
      },
      processDate() {
            return {
                disabledDate: time =>{
                if (this.startTime) {
                    return new Date(this.startTime).getTime() < time.getTime()
                        
                } else {
                    return time.getTime() > Date.now()
                }
                }
            }
      },
      getList() {
        this.loading = true;
        waitedTaskList(this.queryParams).then(response => {
          this.list = response.data;
          this.total = response.page.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          categoryKey: '',
          categoryName: '',
          aliasName: ''
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNo = 1
        this.getList();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.form.id = '';
        this.open = true;
        this.title = "添加任务";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.form = deepClone(row);
        this.open = true;
        this.title = "修改任务";
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id);
        this.single = selection.length !== 1;
        this.multiple = !selection.length;
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        let dictIds = [];
        if (row.deploymentId) {
          dictIds.push(row.deploymentId);
        } else {
          dictIds = this.ids
        }
        this.$confirm('是否确认删除?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return deleteDefinition({'ids' : dictIds});
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () {
        });
      },
        /** 审批按钮操作 */
      examineAndApprove(row) {
        this.reset();
        this.definitionKey = row.definitionKey;
        this.businessKey = row.businessKey;
        this.id=row.id;
        formDataShow(row.id).then(response => {
          // FormProperty_3qipis2--__!!radio--__!!审批意见--__!!i--__!!同意--__--不同意
          // FormProperty_0lffpcm--__!!textarea--__!!批注--__!!f--__!!null
          let datas = response.data;
          let formData = []
          for (let i = 0; i < datas.length; i++) {
            let strings = datas[i].split('--__!!')
            let controlValue = null
            let controlDefault = null
            switch (strings[1]) {
              case 'radio':
                controlValue = 0;
                controlDefault = strings[4]
                break;
              // default:
            }
            formData.push({
              controlId: strings[0],
              controlType: strings[1],
              controlLable: strings[2],
              controlIsParam: strings[3],
              controlValue: controlValue,
              controlDefault: controlDefault
            })
          }
          this.form.formData = formData;
          this.open = true;
          this.title = "审批";
        });
      },
     /** 提交按钮 */
      submitForm() {
        formDataSave(this.id,this.form.formData).then(response => {
          this.msgSuccess("审批成功");
          this.open = false;
          this.getList();
        });
      },
      change1(){
      this.$forceUpdate()
    }
    }
  };
</script>
